package org.cuckoos.android.ui;

import org.cuckoos.android.R;
import org.cuckoos.android.SocketService;
import org.cuckoos.android.action.sendMsgAction;
import org.cuckoos.android.command.SendMsgCommand;
import org.cuckoos.android.domain.Command;
import org.cuckoos.android.domain.Constant;
import org.cuckoos.android.domain.User;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

/**
 * 用户输入界面
 * 1. 输入/回复消息
 * 2. 发送文件
 * 3. 发送图片,用相机
 * 4. 语音通话
 * @author bugu
 *
 */
public class InputActivity extends Activity implements ChatPanel{
	Button send ;
	Button browse;
	Button call;
	Button video;
	TextView historyField;
	TextView inputField;
	private SendMsgCommand handler;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.input);
		initUI();
		initButtonListener();
		handler = new SendMsgCommand(SocketService.getDefault().getSocket(),this);
		SocketService.getDefault().addHandler(handler);
	}
	private void initUI(){
		send = (Button) this.findViewById(R.id.send);
		browse = (Button) this.findViewById(R.id.browse);
		call = (Button) this.findViewById(R.id.call);
		video = (Button) this.findViewById(R.id.video);
		historyField = (TextView) this.findViewById(R.id.historyField);
		inputField = (TextView) this.findViewById(R.id.inputField);
	}
	
	
	private void initButtonListener(){
		send.setOnClickListener(new sendMsgAction(this));
	}
	
	public void sendMessage(){
		SocketService s = SocketService.getDefault();
		SendMsgCommand smc = new SendMsgCommand(s.getSocket(),getTheOther(),getMsg());
		appendHistory(this.getResources().getString(R.string.locale_me),getMsg());
		smc.execute();
	}
	
	private String getMsg() {
		return inputField.getText().toString();
	}
	public User getMe(){
		return null;
	}
	
	public User getTheOther() {
		return (User) this.getIntent().getExtras().get(Constant.DEFAULT_USER_KEY);
	}
	@Override
	public void showMessage(final Command msg) {
		this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				appendHistory(msg.getUser().getName(),msg.getMsg());
			}

			
		});
	}
	@Override
	protected void onStop() {
		super.onStop();
		SocketService.getDefault().removeHandler(handler);
		handler = null;
	}
	
	
	private void appendHistory(String userName , String msg) {
		String history = historyField.getText()+"\n";
		history += userName+" 说: \n"+ msg;
		historyField.setText(history);
	}
	
	
	

	
}
